<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla security
defined('_JEXEC') or die;
jimport('joomla.application.component.controllerform');
jimport('joomla.database.table');
 
// Controller Textcode view
class JooSamControllerTextcode extends JControllerForm{
	protected $text_prefix = 'Textcode';

	public function save($key = null, $urlVar = null){

		$data = JRequest::getVar('jform', array(), 'post', 'array');
		
		//New String
		if ($data['id'] == '0'){
			$addString = $this->addString();
			if($addString){
				$this->setMessage(JText::_('COM_JOOSAM_TXT_CONT_CREATED'));
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textcodes', false));
			}
			else{
				$this->setMessage(JText::_('COM_JOOSAM_TXT_CONT_CREATED_ERROR'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textcodes', false));
			}
		}
		else{
			$editString = $this->editString();
			if($editString){
				$this->setMessage(JText::_('COM_JOOSAM_TXT_CONT_EDITED'));
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textcodes', false));
			}
			else{
				$this->setMessage(JText::_('COM_JOOSAM_TXT_CONT_EDITED_ERROR'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=textcodes', false));
			}
		}
	}

	protected function addString(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');
		$existCheck = $this->checkExist();
		if($existCheck){
			$servers = JoosamHelper::currSrvEdit();
			$srv_id = $servers['id'];			
			$txt_code = $data['txt_code'];
			$txt_string = $data['txt_string'];

			$db =& JFactory::getDBO();
			$query = "	INSERT INTO #__joosam_textstrings (srv_id,txt_code,txt_string)
						VALUES ('".$srv_id."','".$txt_code."','".$txt_string."')";
			$db->setQuery($query);
			$build = $db->query();
			if($build){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}

	protected function editString(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');

		$existCheck = $this->checkExist();

		if($existCheck){
			$srv_id = $data['srv_id'];
			$txt_code = $data['txt_code'];
			$txt_string = $data['txt_string'];

			$db =& JFactory::getDBO();
			$query = "	UPDATE ".$db->nameQuote('#__joosam_textstrings')."
						SET ".$db->nameQuote('srv_id')." = ".$db->quote($srv_id).",
							".$db->nameQuote('txt_code')." = ".$db->quote($txt_code).",
							".$db->nameQuote('txt_string')." = ".$db->quote($txt_string)."
						WHERE ".$db->nameQuote('id')." = ".$db->quote($data['id']).";";
			$db->setQuery($query);
			$build = $db->query();
			if($build){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}

	protected function checkExist(){
		$data = JRequest::getVar('jform', array(), 'post', 'array');
		$helpid = JoosamHelper::currSrvEdit();
		$txtid1 = $data['id'];
		$srv_id = $data['srv_id'];
		if($srv_id == '0'){ $srv_id = $helpid['id']; }
		$txt_code = $data['txt_code'];
		$txt_string = $data['txt_string'];

		$db =& JFactory::getDBO();
		$query = "
			SELECT ".$db->nameQuote('id')."
			FROM ".$db->nameQuote('#__joosam_textstrings')."
			WHERE ".$db->nameQuote('srv_id')." = ".$db->quote($srv_id)."
				AND ".$db->nameQuote('txt_code')." = ".$db->quote($txt_code)." ; ";
		$db->setQuery($query);
		$txtid2 = $db->loadResult();
		if($txtid2){
			if($txtid2 == $txtid1){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return true;
		}
	}
}